package com.yj.question_project.utils;

import com.yj.question_project.domain.MetroLine;
import com.yj.question_project.domain.VNode;
import com.yj.question_project.question.Ques1;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Auther: starsky1
 * @Date: 2024/01/10 17:43
 * @Description: todo
 */
@Slf4j
public class NodeUtils {

    public static Integer generateId(Map<String, Integer> stationIdMap,
                                     String staName, AtomicInteger globalId) {
        Integer staId = null;
        if(stationIdMap.containsKey(staName)) {
            staId = stationIdMap.get(staName);
        } else {
            stationIdMap.put(staName, globalId.get());
            staId = globalId.get();
            globalId.incrementAndGet();
        }
        return staId;
    }

    public static void initData(Map<Integer, VNode> stationMap,
                                Map<String, Integer> stationIdMap, Map<String, MetroLine> lineMap) {
        AtomicInteger globalId = new AtomicInteger(0);
        Properties properties = new Properties();
        try {
            final InputStream inputStream = Ques1.class.getResourceAsStream("/station.properties");
            final InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("utf-8"));
            properties.load(inputStreamReader);
            inputStreamReader.close();
            inputStream.close();
        } catch (IOException exception) {
            log.error("io exception, errorMsg: "+exception.getMessage(), exception);
        }

        lineMap.put("line1", new MetroLine("line1", "1号线", "#EECD3D"));
        lineMap.put("line2", new MetroLine("line2", "2号线", "#086394"));
        lineMap.put("line3", new MetroLine("line3", "3号线", "#E89C41"));
        lineMap.put("line3_1", new MetroLine("line3", "3号线", "#E89C41"));
        lineMap.put("line4", new MetroLine("line4", "4号线", "#0B7A49"));
        lineMap.put("line5", new MetroLine("line5", "5号线", "#C5033F"));
        lineMap.put("line6", new MetroLine("line6", "6号线", "#6E194F"));
        lineMap.put("line7", new MetroLine("line7", "7号线", "#8ED400"));
        lineMap.put("line8", new MetroLine("line8", "8号线", "#008589"));
        lineMap.put("line9", new MetroLine("line9", "9号线","#66C997"));
        lineMap.put("line13", new MetroLine("line13", "13号线", "#878603"));
        lineMap.put("line14", new MetroLine("line14", "14号线", "#7C2312"));
        lineMap.put("line14_1", new MetroLine("line14", "14号线", "7C2312"));
        lineMap.put("line18", new MetroLine("line18", "18号线", "#3535AF"));
        lineMap.put("line21", new MetroLine("line21", "21号线", "#260D50"));
        lineMap.put("line22", new MetroLine("line22", "22号线", "#C9532F"));
        lineMap.put("lineGf", new MetroLine("lineGf", "广佛号线", "#B7D402"));
        lineMap.put("lineApm", new MetroLine("lineApm", "Apm号线", "#00B7DF"));

        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            final String lineName = (String) entry.getKey();
            final String value = (String) entry.getValue();
            String[] stas = value.split(",");
            for (int i = 0; i < stas.length; i++) {
                Integer id = generateId(stationIdMap, stas[i], globalId);
                VNode node = stationMap.getOrDefault(id, new VNode(id, stas[i]));
                node.addLine(lineMap.get(lineName));
                lineMap.get(lineName).addStation(node);
                stationMap.put(id, node);
                // 设置前驱
                if(i>0) {
                    node.addRelateVNode(stationMap.get(stationIdMap.get(stas[i-1])));
                }
                // 设置后继
                if(i<stas.length-1) {
                    Integer nextId = stationIdMap.get(stas[i+1]);
                    if(nextId == null) {
                        nextId = generateId(stationIdMap, stas[i+1], globalId);
                        stationMap.put(nextId, new VNode(nextId, stas[i+1]));
                    }
                    node.addRelateVNode(stationMap.get(nextId));
                }
            }
        }
        // 打印邻接链表
        // 排序
//        System.out.println(globalId);
//        List<VNode> list = stationMap.values().stream()
//                .sorted((v1,v2) -> v2.getRelateNodes().size()-v1.getRelateNodes().size()).collect(Collectors.toList());
//        for (VNode node : list) {
//            System.out.println(node);
//        }


    }
}
